{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "c93d5c47",
      "metadata": {},
      "source": [
        "# 导入"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4dcbfd7d-99e4-4c6f-9f07-6fcbdf07e9fe",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "!pip install sentence_transformers"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "adbc6a5c-b127-400d-98a1-6d3001c82219",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "!HF_ENDPOINT=https://hf-mirror.com huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "da0d3900",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "from sentence_transformers import SentenceTransformer\n",
        "import numpy as np"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "27a9041f",
      "metadata": {},
      "source": [
        "# 加载文件"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "fedbdc84-c52f-4552-8945-ea17ec0bbd85",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "# step1 加载文件\n",
        "with open('文本.txt', 'r', encoding='utf-8') as file:\n",
        "  docs = file.readlines()\n",
        "print('文本条数: ', len(docs))\n",
        "print('预览第一条: ', docs[0])"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "45994fa9",
      "metadata": {},
      "source": [
        "# 生成词向量"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "80e65b4d-6713-4e3c-acf0-f989a5db8e35",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "embedding_model = SentenceTransformer(\"/root/.cache/huggingface/hub/models--sentence-transformers--paraphrase-multilingual-MiniLM-L12-v2/snapshots/bf3bf13ab40c3157080a7ab344c831b9ad18b5eb\")\n",
        "embeddings = embedding_model.encode(docs, show_progress_bar=True)\n",
        "print(type(embeddings), embeddings.shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7d965cf2",
      "metadata": {},
      "source": [
        "# 保存"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f78d7f2a-a39b-4dd5-a83e-04fbf721682e",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "np.save('emb.npy', embeddings)\n",
        "embeddings = np.load('emb.npy') # 加载回来，看一下效果\n",
        "print(type(embeddings), embeddings.shape)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
